/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.hcht.xrlh.jcjg.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hcht.xrlh.jcjg.entity.DrJg;
import com.hcht.xrlh.jcjg.service.DrJgService;
import com.hcht.xrlh.rw.entity.DrRw;
import com.hcht.xrlh.rw.service.DrRwService;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;

/**
 * 检查结果Controller
 * @author 检查结果
 * @version 2016-12-23
 */
@Controller
@RequestMapping(value = "${adminPath}/jcjg/drJg")
public class DrJgController extends BaseController {

	@Autowired
	private DrJgService drJgService;

	@Autowired
	private DrRwService drRwService;
	
	@ModelAttribute
	public DrJg get(@RequestParam(required=false) String id) {
		DrJg entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = drJgService.get(id);
		}
		if (entity == null){
			entity = new DrJg();
		}
		return entity;
	}
	
	@RequiresPermissions("jcjg:drJg:view")
	@RequestMapping(value = {"list", ""})
	public String list(DrJg drJg, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DrJg> page = drJgService.findPage(new Page<DrJg>(request, response), drJg); 
		model.addAttribute("page", page);
		return "xrlh/jcjg/drJgList";
	}

	//@RequiresPermissions("jcjg:drJg:view")
	//@RequiresPermissions("vrw:vrw:view")
	@RequestMapping(value = "form")
	public String form(DrJg drJg, Model model) {
		DrRw rw = drRwService.get(drJg.getRwId());
		String dxNames = drJgService.getdxNames(drJg);
		model.addAttribute("drJg", drJg);
		model.addAttribute("dxNames", dxNames);
		model.addAttribute("rwmc", rw.getRwMc());
		
		return "xrlh/jcjg/drJgForm";
	}

	@RequestMapping(value = "viewForm")
	public String viewForm(DrJg drJg, Model model) {
		DrRw rw = drRwService.get(drJg.getRwId());
		String dxNames = drJgService.getdxNames(drJg);
		model.addAttribute("drJg", drJg);
		model.addAttribute("dxNames", dxNames);
		model.addAttribute("rwmc", rw.getRwMc());
		
		return "xrlh/jcjg/drJgFormView";
	}

	
	//@RequiresPermissions("jcjg:drJg:edit")
	@RequiresPermissions("vrw:vrw:view")
	@RequestMapping(value = "save")
	public String save(DrJg drJg, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, drJg)){
			return form(drJg, model);
		}
		drJgService.save(drJg);
		//修改任务状态
		DrRw rw = drRwService.get(drJg.getRwId());
		rw.setRwFlag("01");
		drRwService.update(rw);
		addMessage(redirectAttributes, "检查结果保存成功");
		return "redirect:"+Global.getAdminPath()+"/vrw/vrw/list";
	}
	
	@RequiresPermissions("jcjg:drJg:edit")
	@RequestMapping(value = "delete")
	public String delete(DrJg drJg, RedirectAttributes redirectAttributes) {
		drJgService.delete(drJg);
		addMessage(redirectAttributes, "删除检查结果成功");
		return "redirect:"+Global.getAdminPath()+"/rw/drRw/list";
	}

}